/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.query.builder;

import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.openinsula.vulcano.orm.dao.query.DistinctDaoQuery;
import org.openinsula.vulcano.orm.dao.query.ProjectionDaoQuery;
import org.openinsula.vulcano.orm.dao.query.SimpleDaoQuery;
import org.openinsula.vulcano.orm.dao.query.projection.CountProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MaxProjection;
import org.openinsula.vulcano.orm.dao.query.projection.MinProjection;
import org.openinsula.vulcano.orm.dao.query.projection.Projection;
import org.openinsula.vulcano.orm.dao.query.projection.SumProjection;
import org.openinsula.vulcano.orm.dao.query.spec.Spec;
import org.springframework.util.Assert;

public class DaoQueryBuilder {
	boolean distinct;
	Projection projection;
	FromBuilder fromBuilder = new FromBuilder(this);
	int limit;
	int firstResult;
	boolean lock;

	public Methods selectForUpdate() {
		lock = true;
		return new Methods();
	}

	public Methods select() {
		return new Methods();
	}

	public DaoQuery getDaoQuery() {
		DaoQuery daoQuery = createDaoQuery();
		configureDaoQuery(daoQuery);

		return daoQuery;
	}

	private DaoQuery createDaoQuery() {
		Assert.notNull(fromBuilder.type, "Search Class missing!");

		DaoQuery daoQuery = null;

		if (distinct) {
			daoQuery = new DistinctDaoQuery(fromBuilder.type);

		} else if (projection != null) {
			daoQuery = new ProjectionDaoQuery(fromBuilder.type, projection);

		} else {
			daoQuery = new SimpleDaoQuery(fromBuilder.type);
		}

		return daoQuery;
	}

	private void configureDaoQuery(final DaoQuery daoQuery) {
		Spec spec = fromBuilder.whereBuilder.spec;
		
		if (spec != null) {
			daoQuery.setSpec(spec);
		}
		
		if (firstResult > 0) {
			daoQuery.setFirstResult(firstResult);
		}

		if (limit > 0) {
			daoQuery.setLimit(limit);
		}

		daoQuery.setLock(lock);

		if (projection == null) {
			FilterBuilder filterBuilder = fromBuilder.whereBuilder.filterBuilder;
			daoQuery.setOrderParameters(filterBuilder.orderByFields);
		}

	}

	public class Methods {

		public FromBuilder distinct() {
			distinct = true;
			return fromBuilder;
		}

		public FromBuilder count() {
			projection = new CountProjection();
			return fromBuilder;
		}

		public FromBuilder max(final String propertyName) {
			projection = new MaxProjection(propertyName);
			return fromBuilder;
		}

		public FromBuilder min(final String propertyName) {
			projection = new MinProjection(propertyName);
			return fromBuilder;
		}

		public FromBuilder sum(final String propertyName) {
			projection = new SumProjection(propertyName);
			return fromBuilder;
		}

		public WhereBuilder from(final Class<?> type) {
			return fromBuilder.from(type);
		}

		public FromBuilder limit(final int limit) {
			return interval(0, limit);
		}

		public FromBuilder firstResult(final int firstResult) {
			return interval(firstResult, 0);
		}

		public FromBuilder interval(final int first, final int maxResult) {
			firstResult = first;
			limit = maxResult;
			return fromBuilder;
		}

	}

}
